package br.ucb.sigplan.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.ucb.sigplan.beans.Conteudo;
import br.ucb.sigplan.beans.Curso;
import br.ucb.sigplan.beans.Disciplina;
import br.ucb.sigplan.beans.Professor;
import br.ucb.sigplan.beans.Turma;
import br.ucb.sigplan.connection.FactoryCon;
import br.ucb.sigplan.crud.ConteudoTurmaCrud;
import br.ucb.sigplan.dao.TurmaDAO;
import br.ucb.sigplan.exception.DAOException;
import br.ucb.sigplan.util.TurmaUtil;

public class TurmaDAOImpl implements TurmaDAO{
	private final String SQL_INSERT = "insert into turma (codigo, horario ,semestre,ano,peridoMinistrado, iddisciplina,idprofessor, diaSemana, idcurso) values (?,?,?,?,?,?,?,?,?);";
	private final String SQL_FINDALL = "select * from turma t, disciplina d, professor p where t.iddisciplina = d.iddisciplina and t.idprofessor = p.idprofessor";
	private final String SQL_SEARCHBYID = "select * from turma t, disciplina d, professor p where  idturma = ? and t.iddisciplina = d.iddisciplina";
	private final String SQL_SEARCHBYIDPROF = "select * from turma t, disciplina d, professor p where  p.idprofessor = ? and t.iddisciplina = d.iddisciplina and t.idprofessor = p.idprofessor";
	private final String SQL_DELETE = "delete from turma where idturma = ?";
	private final String SQL_UPDATE = "update turma set codigo = ?, horario = ?, semestre = ?, ano = ?, peridoMinistrado = ?, iddisciplina = ?, idprofessor = ?, diaSemana = ?, idcurso = ? where idturma = ?";

	public List<Turma> findAll() throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		ResultSet set = null;
		List<Turma> turmas = new ArrayList<Turma>();
		Turma turma;
		try {
			instrucao = con.prepareStatement(SQL_FINDALL);
			set = instrucao.executeQuery();
			while (set.next()) {
				turma = new Turma();
				putTurma(set, turma);
				turmas.add(turma);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao, set);
		}
		return turmas;
	}

	public void insert(Turma turma) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_INSERT);
			instrucao.setString(1, turma.getCodigo());
			instrucao.setString(2, turma.getHorario());
			instrucao.setString(3, turma.getSemestre().toString());
			instrucao.setInt(4, turma.getAno());
			instrucao.setString(5, turma.getPeriodoMinistrado());
			instrucao.setInt(6, turma.getDisciplina().getIdDisciplina());
			instrucao.setInt(7, turma.getProfessor().getIdProfessor());
			instrucao.setString(8, TurmaUtil.transformDiasSemanaToString(turma.getDiasSemana()));
			instrucao.setInt(9, turma.getCurso().getIdCurso());
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroInsercao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
	}

	public Turma findTurmaById(Integer idturma) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		ResultSet set = null;
		Turma turma = new Turma();
		try {
			instrucao = con.prepareStatement(SQL_SEARCHBYID);
			instrucao.setInt(1, idturma);
			set = instrucao.executeQuery();
			while (set.next()) {
				turma = new Turma();
				putTurma(set, turma);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao, set);
		}
		return turma;
	}
	
	public void putTurma(ResultSet set, Turma turma) throws SQLException {
		turma.setIdTurma(set.getInt("idTurma"));
		turma.setAno(set.getInt("ano"));
		turma.setCodigo(set.getString("codigo"));
		turma.setProfessor(getProfessor(set));
		turma.setDisciplina(getDisciplina(set));
		turma.setHorario(set.getString("horario"));
		turma.setSemestre(Integer.parseInt(set.getString("semestre")));
		turma.setCurso(getCurso(set));
		turma.setPeriodoMinistrado(set.getString("peridoMinistrado"));
		turma.setConteudos(getConteudos(set));
		turma.setDiasSemana(TurmaUtil.tranformStringToDiasSemana(set.getString("diaSemana")));
	}
	
	private Curso getCurso(ResultSet set) throws SQLException {
		Curso curso = new CursoDAOImpl().findById(set.getInt("idcurso"));
		return curso;
	}
	
	private Professor getProfessor(ResultSet set) throws SQLException {
		Professor professor = new ProfessorDAOImpl().findById(set.getInt("idprofessor"));
		return professor;
	}
	
	private Disciplina getDisciplina(ResultSet set) throws SQLException {
		Disciplina disciplina = new Disciplina();
		new DisciplinaDAOImpl().putDisciplina(disciplina, set);
		return disciplina;
	}
	
	private List<Conteudo> getConteudos(ResultSet set) throws SQLException {
		return ConteudoTurmaCrud.findConteudoByTurma(set.getInt("idturma"));
	}

	@Override
	public List<Turma> findByProfessor(Integer idProfessor) throws DAOException {
		List<Turma> turmas = new ArrayList<Turma>();
		Turma turma;
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		ResultSet set = null;
		try {
			instrucao = con.prepareStatement(SQL_SEARCHBYIDPROF);
			instrucao.setInt(1, idProfessor);
			set = instrucao.executeQuery();
			while (set.next()) {
				turma = new Turma();
				putTurma(set, turma);
				turmas.add(turma);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao, set);
		}
		return turmas;
	}

	@Override
	public void delete(Integer idTurma) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_DELETE);
			instrucao.setInt(1, idTurma);
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("turmaDelecao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
		
	}
	
	@Override
	public void update(Turma turma) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_UPDATE);
			instrucao.setString(1, turma.getCodigo());
			instrucao.setString(2, turma.getHorario());
			instrucao.setString(3, turma.getSemestre().toString());
			instrucao.setInt(4, turma.getAno());
			instrucao.setString(5, turma.getPeriodoMinistrado());
			instrucao.setInt(6, turma.getDisciplina().getIdDisciplina());
			instrucao.setInt(7, turma.getProfessor().getIdProfessor());
			instrucao.setString(8, TurmaUtil.transformDiasSemanaToString(turma.getDiasSemana()));
			instrucao.setInt(9, turma.getCurso().getIdCurso());
			instrucao.setInt(10, turma.getIdTurma());
			if (instrucao.executeUpdate() != 1) {
				throw new DAOException("erroAtualizacao");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroAtualizacao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);	
		}
	}
	
}
